package vaani.internal.actionmanagers;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import vaani.internal.ProcessInstance;

/**
 *
 * @author sourcemorph
 */
public class OpenManager implements ActionManager {
    
    private AggregateManager actualManager = null;
    private static List<String> wordList1;
    private static List<String> wordList2;

    static {
        wordList1 = new LinkedList<String>();
        wordList2 = new LinkedList<String>();

        wordList1.add("application");
        wordList1.add("program");

        wordList2.add("file");
        wordList2.add("document");
        wordList2.add("folder");
    }

    /*this method will return the first word it finds from the query list
     * in one of the predefined lists*/
    private String inList(List<String> query, List<String> list) {
        for(String q : query) {
            if(list.contains(q)) {
                return q;
            }
        }
        return null;
    }


    public OpenManager(List<String> arguments) {
        String temp = null;
        actualManager = new AggregateManager();
        if((temp = inList(arguments, wordList1)) != null) {
            List<String> tempList = new LinkedList<String>(arguments);
            tempList.remove(temp);
            actualManager.mergeActionManager(new ApplicationManager(tempList));
        }
        if((temp = inList(arguments, wordList2)) != null) {
             List<String> tempList = new LinkedList<String>(arguments);
             tempList.remove(temp);
             actualManager.mergeActionManager(new FileManager(tempList));
        }
        if(actualManager.getProcessInstance().size() == 0) {
            actualManager.mergeActionManager(new ApplicationManager(arguments));
            actualManager.mergeActionManager(new FileManager(arguments));
        }
    }

    public List<ProcessInstance> getProcessInstance() {
        if(! actualManager.equals(null)) {
            List<ProcessInstance> temp = actualManager.getProcessInstance();
            //Collections.sort(temp);
            return temp;
        }
        return null;
    }

    public boolean runProcess() {
        return runProcess(0);
    }

    public boolean runProcess(int x) {
        if(! actualManager.equals(null)) {
            return actualManager.runProcess(x);
        }
        return false;
    }
}
